#include "adr/Metadata/Validators/StringLengthAttribute.h"
#include "adr/Metadata/Property.h"
#include <string>

namespace adr {
	using namespace std;

	StringLengthAttribute::StringLengthAttribute(int maxLength, int minLength)
		: ValidationAttribute("StringLengthAttribute"), maxLength(maxLength), minLength(minLength) {}

	bool StringLengthAttribute::Validate(BaseProperty& prop) {		
		Property<string>& strProp = dynamic_cast<Property<string>&>(prop);

		if (!strProp.HasValue()) {
			return true;
		}

		auto strLength = strProp.GetValue()->length();

		if (strLength < this->minLength) {
			this->AddValidationError(prop, "The field " + prop.GetDisplayName() + " must have its length greater than " + to_string(this->minLength));
			return false;
		}
		
		if (strLength > this->maxLength) {
			this->AddValidationError(prop, "The field " + prop.GetDisplayName() + " exceeds the length of " + to_string(this->maxLength) + " characters");			
		}

		return false;
	}
}